#-*-coding:utf-8-*-
import sys
import os
import shutil
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from Widgets.palette_color import showColor

class FileListWidget(QListWidget):

    def __init__(self):
        super(FileListWidget,self).__init__()
        self.setPalette(showColor('bg'))

        #全局变量
        self.current_real_shot_path=''#当前镜头目录路径
        # self.current_real_sample_path=''#当前镜头对应的sample路径


        #双击事件
        self.doubleClicked.connect(self.__openFile)

        #添加右击菜单栏
        self.setContextMenuPolicy(Qt.CustomContextMenu)
        self.customContextMenuRequested.connect(self.__rightMenu)


        self.setAcceptDrops(True)
        # self.setDragEnabled(True)

        self.mainList=[]




    def dragEnterEvent(self, event):
        text=event.mimeData().text()
        filePath=text[8:]#拖入的文件路径
        if self.current_real_shot_path=='':
            QMessageBox.information(self,'提示','请先选择具体镜头')
        else:
            sample_path=os.path.join(self.current_real_shot_path,'sample')

            if os.path.isfile(filePath) and self.__submitFilter(filePath):
                filename=self.getFileNameByPath(filePath)
                sample_file_path = os.path.join(sample_path, filename)
                if filename not in self.mainList:
                    shutil.copyfile(filePath, sample_file_path)
                    self.addToList(filename)
                else:
                    reply=QMessageBox.question(self, '警告', '已存在该文件，是否覆盖？',QMessageBox.Yes|QMessageBox.No,QMessageBox.No)
                    if reply==QMessageBox.Yes:
                        os.remove(sample_file_path)
                        shutil.copyfile(filePath,sample_file_path)

            else:
                QMessageBox.information(self,'提示','请拖入单个视频/图片')


    def getFileNameByPath(self,path):
        '''
        根据给的路径，返回文件路径
        :param path:
        :return:
        '''
        dirpath,name=os.path.split(path)
        return name

    def setList(self,namelist):
        '''
        设置整个显示列表内容
        :param namelist:
        :return:
        '''

        self.clear()
        self.mainList=namelist
        self.addItems(namelist)


    def addToList(self,name):
        '''
        将名称加入到self.mainList当中，然后更新
        :param name:
        :return:
        '''
        if name not in self.mainList:
            self.mainList.append(name)
            self.setList(self.mainList)

        else:
            QMessageBox.information(self,"提示","该文件已存在，请更新版本号再提交")



    def sendChooseName(self):
        current_item=self.currentItem()
        name=current_item.text()
        print(name)

    def __rightMenu(self):
        self.menu=QMenu()
        self.openAction=QAction(u'打开目录',self)
        self.openAction.triggered.connect(self.__openDir)
        self.menu.addAction(self.openAction)

        self.menu.exec_(QCursor.pos())


    def __submitFilter(self,path):
        '''
        过滤函数，当选中的是视频或者图片时
        :param path:
        :return:
        '''
        filter_list=['.wmv','.asf','.asx','.rm','.rmvb','.mpg','.mpeg','.mpe','.3gp','.mov','.mp4','.m4v','.avi','.dat','.mkv','.flv','.vob',
                     '.jpeg','.jpg','.png','.bmp','.gif','.psd','.pdd','.psdt','.tiff','.tiff']
        fileType=os.path.splitext(path)[1]
        if fileType in filter_list:
            return True
        else:
            return False


    def setCurrentShotPath(self,current_shot_path):
        '''
        设置当前镜头路径
        :param current_shot_path:
        :return:
        '''
        self.current_real_shot_path=current_shot_path



    def __openFile(self):
        '''
        双击就打开文件
        :return:
        '''
        filename=self.currentItem().text()
        file_path=os.path.join(self.current_real_shot_path,'sample/%s'% filename)
        if file_path:
            os.startfile(file_path)

    def __openDir(self):
        '''
        打开当前目录
        :return:
        '''
        current_sample_path=os.path.join(self.current_real_shot_path,'sample')
        try:
            os.startfile(current_sample_path)
        except:
            pass

if __name__ == '__main__':
    app=QApplication(sys.argv)
    F=FileListWidget()
    nameList=['a','b','c','d']
    F.setList(nameList)
    F.show()
    sys.exit(app.exec_())